<style scoped>
  .sellform >>> .weui-form-preview__hd .weui-form-preview__value {
    font-size: 1.2em;
  }
  .sellform {
    background: #e3e3e3;
  }
  .page-infinite-wrapper {
    background: #e3e3e3;
  }
  .sellform >>> .weui-cells {
    margin-top: 0;
    background: #e3e3e3;
  }
  .sellform >>> .weui-loadmore__tips {
    background: #e3e3e3;;
  }
  .sellform >>> .weui-loadmore {
    margin-bottom: 0;
  }
  .weui-form-preview {
    margin: 5px;
    border-radius: 5px;
  }

  .rj-cell-child {
    display: inline-block;
    font-size: 0;
    padding: 5px 15px;
    text-align: left;
  }
  .rj-cell-child.max span{
    font-size: 16px;
    padding-right: 20px;
  }
  .rj-cell-child.minpad{
    padding-right: 10px !important;
  }
  .rj-cell-child label {
    color: #333;
    font-size: 14px;
  }
  .rj-cell-child span {
    padding-left: 20px;
    font-size: 14px;
    color: #333;
  }
  .rj-cell-child .tip {
    font-size: 16px;
    color: #333;
  }
  .rj-cell-child i {
    font-size: 18px;
    font-style: normal;
    color: #333;
  }
  .rj-cell-child-tit {
    background: #eee;
    font-size: 14px;
    padding: 5px 15px;
    border-bottom: 1px solid #ddd;
  }
  .weui-form-preview__ft .weui-form-preview {
    background: #f5f5ef;
  }
  .weui-form-preview__ft {
    line-height: normal;
  }



  .weui_tab_bd {
    background: #eee;
  }
  .weui-form-preview {
    background-color: #fff;
    margin: 5px;
    border-radius: 5px;
  }
  .weui-form-preview__hd label, .weui-form-preview__hd em {
    font-size: .9em !important;
  }
  .weui_navbar {
    white-space: nowrap;
    overflow: scroll;
    display: block !important;
    border-bottom: 1px solid #BCBAB6;
    font-size: 0;
    /*background: #fafafa;*/
  }
  .weui_navbar_item {
    font-size: 14px;
    display: inline-block;
    width: 33.33%;
    padding: 13px 0;
    background: #fff;
    position: relative;
    color: #888;
  }
  .weui_navbar_item.weui_bar_item_on {
    background-color: #f5f5f5;
    color: #00913a;
  }
  .weui_navbar:after {
    border-bottom: 0;
  }
  .weui_navbar_item:after {
    content: " ";
    position: absolute;
    right: 0;
    top: 0;
    width: 1px;
    height: 100%;
    border-right: 1px solid #cccccc;
    color: #cccccc;
    -webkit-transform-origin: 100% 0;
    transform-origin: 100% 0;
    -webkit-transform: scaleX(0.5);
    transform: scaleX(0.5);
    font-size: 10px;
  }
  .weui-panel__bd {
    text-align: left;
  }
  .hoverbar {
    background: #03A9F4;
    position: fixed;
    bottom: 100px;
    right: 20px;
    height: 50px;
    line-height: 50px;
    width: 50px;
    z-index: 100;
    font-size: 30px;
    border-radius: 50%;
    box-shadow: 0px 2px 3px rgba(100, 100, 100, 0.5);
    color: #fff;
  }
  .delsty {
    background: #cfd4d6;
    position: relative;
    top:5px;
    left:90%;
    bottom: 100px;
    right: 20px;
    height: 20px;

    width: 20px;
    z-index: 100;
    font-size: 15px;
    border-radius: 50%;
    box-shadow: 0px 2px 3px rgba(100, 100, 100, 0.5);
    color: #fff;
    text-align:center;
  }
  .weui-prompt-input {
    height: 56px !important;
  }
  .weui-prompt-input-my {
    padding: 4px 6px;
    border: 1px solid #ccc;
    box-sizing: border-box;
    height: auto;
    width: 80%;
    margin-top: 10px;
  }
</style>

<template>
  <div class="pickinglist">
    <div class="hoverbar" v-for="(permission, index) in permissions" v-show="permission.permss.scopetype ==1 && permission.permission == 4"  @click="addItem">+</div>
    <SearchBar @doSearch="doSearch" />
    <div class="weui_tab">
      <div class="weui_navbar order-tabhd">
        <a class="weui_navbar_item" :class="{ weui_bar_item_on: postParams.status == 1 }" href="javaScript:;" @click="switchStatus(1)">待确认</a>
        <a class="weui_navbar_item " :class="{ weui_bar_item_on: postParams.status == 2 }" href="javaScript:;" @click="switchStatus(2)">待出(入)库</a>
        <a class="weui_navbar_item " :class="{ weui_bar_item_on: postParams.status == 3 }" href="javaScript:;" @click="switchStatus(3)">已出(入)库</a>
      </div>
      <div class="weui_tab_bd" style="background-color: #fff">
        <div class="page-infinite-wrapper" ref="wrapper" :style="{ height: wrapperHeight + 'px' }" >
          <mescroll-vue ref="mescroll" id="wrapper" :down="mescrollDown" :up="mescrollUp" @init="mescrollInit">
            <div class="weui-form-preview" v-for="(item, index) in data" :key="item.guid" :ref="item.guid">
              <div class="delsty"   v-show="item.operatorid == userid"  @click="delItem(item.guid)">X
                <!--      <img src="../../../static/image/icon-addr.png" width="40" />-->
              </div>


              <div class="weui-form-preview__hd" @click="viewItem(item.guid)">
                <label class="weui-form-preview__label">编号</label>
                <em class="weui-form-preview__value">{{item.code}}</em>
              </div>
              <div class="weui-form-preview__bd">
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">公司</label>
                  <span class="weui-form-preview__value">{{item.orgname}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">库房</label>
                  <span class="weui-form-preview__value">{{item.srname}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">库间</label>
                  <span class="weui-form-preview__value">{{item.shname}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">产品</label>
                  <span class="weui-form-preview__value">{{item.pname}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">规格</label>
                  <span class="weui-form-preview__value">{{item.spename}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">重量(吨)</label>
                  <span class="weui-form-preview__value">{{item.weight}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">调整类型</label>
                  <span class="weui-form-preview__value">{{item.type == 1 ? '出库' : '入库'}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">状态</label>
                  <span class="weui-form-preview__value">{{status2Txt(item.status, item.type)}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">申请人</label>
                  <span class="weui-form-preview__value">{{item.operatorname}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">备注</label>
                  <span class="weui-form-preview__value">{{item.remark}}</span>
                </div>
                <div class="weui-form-preview__item">
                  <label class="weui-form-preview__label">创建时间</label>
                  <span class="weui-form-preview__value">{{item.createdate}}</span>
                </div>
              </div>
              <div class="weui-form-preview__ft" style="line-height: 50px;" v-for="(permission, index) in permissions">
                <a class="weui-form-preview__btn weui-form-preview__btn_primary" href="javascript:" v-show="permission.permss.functiontype.indexOf(2) > -1  && permission.permss.scopetype ==1  && item.status == 1 && permission.srguid == item.srguid && permission.permission == 5" @click="outstorerm(item.guid)" >确认</a>
                <a class="weui-form-preview__btn weui-form-preview__btn_primary" href="javascript:" v-show="permission.permss.functiontype.indexOf(2) > -1  && permission.permss.scopetype ==1  && item.status == 2 && permission.srguid == item.srguid && permission.permission == 4 && item.type == 1" @click="doAct('out', item.guid)" >出库</a>
                <a class="weui-form-preview__btn weui-form-preview__btn_primary" href="javascript:" v-show="permission.permss.functiontype.indexOf(2) > -1  && permission.permss.scopetype ==1  && item.status == 2 && permission.srguid == item.srguid && permission.permission == 4 && item.type == 2" @click="doAct('in', item.guid)" >入库</a>
              </div>
            </div>
          </mescroll-vue>
        </div>
      </div>
    </div>
  </div>
</template>

<script type="text/babel">
  import MescrollVue from 'mescroll.js/mescroll.vue'
  import SearchBar from '../common/SearchBar'
  export default {
    name: 'ChangesaList',
    components: {SearchBar, MescrollVue},
    data() {
      return {
        openid: localStorage.openid,
        userid: localStorage.userid,
        permissions:[],
        postParams: {
          status: 1
        },
        mescroll: null, // mescroll实例对象
        mescrollDown: {},
        mescrollUp: { // 上拉加载的配置.
          callback: this.upCallback, // 上拉回调,此处简写; 相当于 callback: function(page, mescroll) { }
          page: {
            num: 0, //当前页 默认0,回调之前会加1; 即callback(page)会从1开始
            size: 5 //每页数据条数,默认10
          },
          htmlNodata: '<p class="upwarp-nodata">-- 我是有底线的 --</p>',
          noMoreSize: 1,
          empty: {
            warpId: "wrapper", //父布局的id (1.3.5版本支持传入dom元素)
            tip: "暂无相关数据~" //提示
          }
        },
        wrapperHeight: 0,
        data: [],
      }
    },
    created() {
      window.addEventListener('setItem', ()=> {
        this.openid = localStorage.getItem('openid');
        this.userid = localStorage.getItem('userid');
      })
    },
    mounted() {
      //获取权限
      this.getPermission();
      //根据内容高度适配wraper高度
      this.wrapperHeight = document.documentElement.clientHeight - this.$refs.wrapper.getBoundingClientRect().top;
    },
    methods: {
      addItem: function() {
        this.$router.push({name: 'AddChangesa'});
      },
      status2Txt: function(status, type) {
        let statusName = ''
        switch (status) {
          case 1:
            statusName = '待确认';
            break;
          case 2:
            if (type == 1) {
              statusName = '待出库';
            } else {
              statusName = '待入库';
            }
            break;
          case 3:
            if (type == 1) {
              statusName = '已出库';
            } else {
              statusName = '已入库';
            }
            break;
        }
        return statusName;
      },
      getPermission: function() {
        this.axios({
          method: 'get',
          params:{opttype: 5},
          url: '/produce/OperatorPermission!getOperPerm.action?openid=' + localStorage.openid,
        }).then((response) => {
          if (response.data.msg == 'success') {
            this.$nextTick(() => {
              this.permissions = response.data.permission;
              console.log('this.permissions:', this.permissions)
            })
          } else {
            this.toastError("系统错误");
          }

        })
      },
      switchStatus: function (sta) {
        this.postParams.status = sta;
        this.getPageData();
      },
      mescrollInit (mescroll) {
        this.mescroll = mescroll  // 如果this.mescroll对象没有使用到,则mescrollInit可以不用配置
      },
      upCallback (page, mescroll) {
        this.postParams.pageNo = page.num;
        this.postParams.pageSize = page.size;
        // 联网请求
        this.axios({
          method: 'get',
          url: '/produce/Changestoreapply!list.action?openid=' + localStorage.openid,
          params: this.postParams
        }).then((response) => {
          if (response.data.status == 'true') {
            let res = response.data.result != undefined ? response.data.result : {list: {rows: []}};
            // 如果是第一页需手动置空列表
            if (page.num === 1) this.data = []
            // 把请求到的数据添加到列表
            this.data = this.data.concat(res.list.rows);
            // 数据渲染成功后,隐藏下拉刷新的状态
            this.$nextTick(() => {
              mescroll.endSuccess(res.list.rows.length)
            })
            //将获取上一访问列表临时页码恢复正常
            if (this.mescrollUp.page.size > 5) {
              this.mescrollUp.page.num = this.$store.state.list.pageNo;
              this.mescrollUp.page.size = 5;
            }
          } else if (response.data.status == 'false') {
            this.toastError(response.data.result);
          } else {
            this.toastError("系统错误");
          }

        }).catch((e) => {
          // 联网失败的回调,隐藏下拉刷新和上拉加载的状态;
          mescroll.endErr()
        })
      },
      getPageData: function () {
        $('#wrapper').scrollTop(0);
        this.mescroll.resetUpScroll();
      },
      doSearch: function(keywords) {
        this.postParams.search = keywords;
        this.getPageData();
      },
      outstorerm: function (guid) {
        let that = this;
        $.prompt({
          title: '审批意见',
          empty: false, // 是否允许为空
          onOK: function (input) {
              that.axios({
                method: 'post',
                url: '/produce/Changestoreapply!confirm.action?openid=' + localStorage.openid,
                params: {guid: guid, desc: input}
              }).then((response) => {
                if (response.data.info == '确认成功!') {
                  that.toastSuccess('确认成功!');
                  //更新列表
                  that.mescroll.resetUpScroll();
                } else {
                  taht.toastText(response.data.info);
                }
              })
          },
          onCancel: function () {
            $('#weui-prompt-input').remove();
          }
        });
        $('#weui-prompt-input').remove();
        $(".weui-prompt-text").after('<textarea  rows="3" style="height: 56px; width: 100%;" class="weui_input weui-prompt-input" id="weui-prompt-input" value=""></textarea>');
      },
      doAct: function(type, id) {
        if (type == 'out') {
          this.$router.push({name: 'ChangesaOutStoreRm', query: {guid: id}});
        } else {
          this.$router.push({name: 'ChangesaFeedStoreRm', query: {guid: id}});
        }
      },
      delItem: function (guid) {
        var that = this;
        $.confirm("确定删除!", function() {
          that.axios({
            method: 'post',
            url: '/produce/Changestoreapply!del.action?openid=' + localStorage.openid,
            params: {guid: guid}
          }).then((response) => {
            if (response.data.info == '数据删除成功') {
              that.$refs[guid][0].remove();
              that.toastSuccess('删除成功！');
            } else {
              that.toastText(response.data.info);
            }

          })
        }, function() {
          //点击取消后的回调函数
        });




      },
    },
    activated () {
      //判断是否是返回上一页
      if (this.$store.state.list.reffer != this.$options.name) return;
      //获取上一次访问列表数据
      this.mescrollUp.page.num = 0;
      this.mescrollUp.page.size = this.$store.state.list.pageNo * this.mescrollUp.page.size;
      this.getPageData();
      //定位
      this.$nextTick(function(){
        let position = this.$store.state.list.postion //返回页面取出来
        $('#wrapper').scrollTop(position);
        let status = this.$store.state.list.status;
        this.postParams.status = status;
      })
    },
    beforeRouteLeave(to, from, next){
      let position = $('#wrapper').scrollTop();
      let status = this.postParams.status;
      this.$store.commit('setPosition', position) //离开路由时把位置存起来
      this.$store.commit('setStatus', status)
      this.$store.commit('setPageNo', this.mescrollUp.page.num);
      this.$store.commit('setReffer', this.$options.name);
      next()
    }
  };
</script>
